//
//  AddMessagePhotoViewCell.m
//  陶瓷
//
//  Created by 王戈 on 15/9/5.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "AddMessagePhotoViewCell.h"
#import "AddMessagePhotoCell.h"

@interface AddMessagePhotoViewCell ()<UICollectionViewDelegate ,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *photosView;
@property (nonatomic ,copy) AddPhotoBlock addPhotoBlock;
@property (nonatomic ,weak) AddMessagePhotoCell *currentCell;

@end

@implementation AddMessagePhotoViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView andImages:(NSArray *)images {
    static NSString *ID = @"AddMessagePhotoViewCell";
    AddMessagePhotoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[AddMessagePhotoViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.images = [images mutableCopy];
        if (!cell.images) {
            cell.images = [NSMutableArray arrayWithCapacity:6];
        }
    }
    return cell;

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addSubview:self.photosView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivePhoto:) name:kTakePhotoCompletNotification object:nil];
    }
    return self;
}

- (UICollectionView *)photosView {
    if (_photosView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(kAddMessagePhotoCellHeight, kAddMessagePhotoCellHeight);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//设置其布局方向
        flowLayout.minimumInteritemSpacing = 15;
        //    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        
        _photosView = [[UICollectionView alloc]initWithFrame:CGRectMake(20 , 0 , SCREEN_WIDTH - 40, kAddMessagePhotoViewCellHeight) collectionViewLayout:flowLayout];
        
        _photosView.dataSource = self;
        _photosView.delegate = self;
        
        _photosView.showsHorizontalScrollIndicator = NO;
        _photosView.showsVerticalScrollIndicator = NO;
        _photosView.backgroundColor = [UIColor clearColor];
        _photosView.showsVerticalScrollIndicator = NO;
        [_photosView registerClass:[AddMessagePhotoCell class] forCellWithReuseIdentifier:@"AddMessagePhotoCell"];
        [self addSubview:_photosView];
    }
    return _photosView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AddMessagePhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddMessagePhotoCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        if (_images.count == 6) {
            NSObject *obj = _images[indexPath.item];
            cell.photoed = YES;
            if ([obj isKindOfClass:[NSString class]]) {
                [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)obj]];
            } else if ([obj isKindOfClass:[UIImage class]]) {
                cell.backgroundImageView.image = (UIImage *)obj;
            }
        } else {
                cell.photoed = NO;
        }
    } else {
        NSObject *obj = _images.count == 6 ? _images[indexPath.item] : _images[indexPath.item - 1];
        cell.photoed = YES;
        if ([obj isKindOfClass:[NSString class]]) {
            [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)obj]];
        } else if ([obj isKindOfClass:[UIImage class]]) {
            cell.backgroundImageView.image = (UIImage *)obj;
        }
    }

//    if (_images.count > indexPath.item) {
//        NSObject *obj = _images[indexPath.item];
//        cell.photoed = YES;
//        if ([obj isKindOfClass:[NSString class]]) {
//            [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)obj]];
//        } else if ([obj isKindOfClass:[UIImage class]]) {
//            cell.backgroundImageView.image = (UIImage *)obj;
//        }
//    } else {
//        cell.photoed = NO;
//    }
    
    [cell deletePhotoBlock:^(AddMessagePhotoCell *cell) {
        NSIndexPath *indexPath = [_photosView indexPathForCell:cell];
        
        if (_images.count == 6) {
            [_images removeObjectAtIndex:indexPath.item];
        } else {
            [_images removeObjectAtIndex:indexPath.item - 1];
        }
        [_photosView reloadData];
    }];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return (_images.count == 6) ? 6 : _images.count + 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AddMessagePhotoCell *cell = (AddMessagePhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (_addPhotoBlock) {
        _addPhotoBlock();
        // 瞅瞅这个找没照相 .关乎之后传来的照片放在哪
        if (cell.isPhotoed) {
            _currentCell = cell;
        }
    }
}

- (void)refreshPhotos {
    [_photosView reloadData];
}

- (void)implementAddPhotoBlock:(AddPhotoBlock)block {
    _addPhotoBlock = block;
}

- (void)receivePhoto:(NSNotification *)noti {
    if (_currentCell) {
        NSIndexPath *indexPath = [_photosView indexPathForCell:_currentCell];
        [_images replaceObjectAtIndex:indexPath.item withObject:noti.object];
        _currentCell = nil;
    } else {
        [_images addObject:noti.object];
    }
    [_photosView reloadData];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
